package com.leicx.guli.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leicx.guli.common.constant.ParamConstant;
import com.leicx.guli.common.utils.PageUtils;
import com.leicx.guli.common.utils.Query;
import com.leicx.guli.product.dao.AttrAttrgroupRelationDao;
import com.leicx.guli.product.entity.AttrAttrgroupRelationEntity;
import com.leicx.guli.product.service.AttrAttrgroupRelationService;
import com.leicx.guli.product.vo.attr.AttrGroupRelationVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Resource
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public AttrAttrgroupRelationEntity getByAttrId(Long attrId) {
        return this.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
    }

    @Override
    public List<AttrAttrgroupRelationEntity> listByAttrGroupId(Long attrGroupId) {
        return this.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq(ParamConstant.ATTR_GROUP_ID, attrGroupId));
    }

    @Override
    public List<AttrAttrgroupRelationEntity> listByAttrGroupIds(List<Long> attrGroupIds) {
        if (CollUtil.isNotEmpty(attrGroupIds)) {
            return this.list(new QueryWrapper<AttrAttrgroupRelationEntity>().in(ParamConstant.ATTR_GROUP_ID, attrGroupIds));
        }
        return new ArrayList<>();
    }

    @Override
    public void attrRelationDelete(AttrGroupRelationVo[] attrGroupRelations) {
        attrAttrgroupRelationDao.attrRelationDelete(attrGroupRelations);
    }

    @Override
    public void attrRelation(AttrGroupRelationVo[] attrGroupRelations) {
        List<AttrAttrgroupRelationEntity> collect = Arrays.stream(attrGroupRelations)
                .map(item -> BeanUtil.copyProperties(item, AttrAttrgroupRelationEntity.class))
                .collect(Collectors.toList());
        saveBatch(collect);
    }

}